<?php

namespace App\Http\Controllers\Backend;

use App\Http\Repositories\ProjectTypeRepository;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\ProjectTypesRequest;
use Illuminate\Http\Request;

class ProjectTypeController extends Controller
{
    protected $projectTypeRepository;

    public function __construct(ProjectTypeRepository $projectTypeRepository)
    {
        $this->projectTypeRepository = $projectTypeRepository;
    }

    /**
     * 获取项目类型列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        return responseResult($this->projectTypeRepository->getList(), self::SUCCESS_CODE);
    }

    /**
     * 添加项目类型
     * @param ProjectTypesRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(ProjectTypesRequest $request)
    {
        $result = $this->projectTypeRepository->edit($request->all());
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 更新项目类型
     * @param ProjectTypesRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(ProjectTypesRequest $request)
    {
        $result = $this->projectTypeRepository->edit($request->except("id"), $request->get("id"));
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 删除项目类型
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function del($id)
    {
        $result = $this->projectTypeRepository->del($id);
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 启用项目类型
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeEnable($id, Request $request)
    {
        $this->validate($request, ['status' => 'required']);
        $result = $this->projectTypeRepository->changeEnable($id, $request->get("status"));
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }
}
